import Log, { Level } from "sap/base/Log";
import Event from "sap/ui/base/Event";
import Controller from "sap/ui/core/mvc/Controller";
import History from "sap/ui/core/routing/History";
import UIComponent from "sap/ui/core/UIComponent";

/**
 * @namespace sap.ui.demo.nav.controller
 */
export class BaseController extends Controller {
  onInit(): void {
    Log.setLevel(Level.INFO);
    this.router.attachBypassed((oEvent: Event) => {
      const sHash = oEvent.getParameter("hash");
      Log.info(
        `Sorry, but the hash "${sHash}" is invalid.`,
        "The resource was not found."
      );
    });
    this.router.attachRouteMatched((oEvent:Event) => {
      const sRouteName = oEvent.getParameter('name');
      Log.info(`User accessed route ${sRouteName}, timestamp= ${new Date().getTime()}`)
    })
  }

  onNavBack() {
    const oHistory = History.getInstance();
    const sPreviousHash = oHistory.getPreviousHash();

    if (sPreviousHash !== undefined) window.history.go(-1);
    else this.router.navTo("appHome", {}, {}, true);
  }

  protected get router() {
    return UIComponent.getRouterFor(this);
  }

  protected get view() {
    return this.getView();
  }
}

// export default BaseController;
